import { invokeRPC } from "@/ui/utils/rpc/helpers";
import type { QuickMessage } from "@common/models";

export const load = () => {
  return invokeRPC({
    rpcName: "app.quickMessage.load",
    rpcParams: {},
    receiver: "plugin",
  }) as Promise<QuickMessage[]>;
};

export const has = (params: { title?: string; shortcut?: string }) => {
  const hasParams = !!Object.values(params).filter((v) => !!v).length;
  if (!hasParams) {
    throw new Error("need one of title/shortcut");
  }

  return invokeRPC({
    rpcName: "app.quickMessage.has",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<{ existed: boolean }>;
};

export const create = (params: { record: QuickMessage }) => {
  return invokeRPC({
    rpcName: "app.quickMessage.create",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<void>;
};

export const remove = (params: { id: QuickMessage["id"] }) => {
  return invokeRPC({
    rpcName: "app.quickMessage.remove",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<void>;
};

export const update = (params: { record: QuickMessage }) => {
  return invokeRPC({
    rpcName: "app.quickMessage.update",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<void>;
};
